package com.httpclient;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class Test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String result = "1#未知消息";
        String pageContent = "";
        String sessionId = "";
        int returnCode = 0;
        GetMethod getFirst = new GetMethod();
        PostMethod post = new PostMethod();
        HttpClient client = new HttpClient();
        Cookie[] cookies = null;
        try {
            // 首先读取登录页面内容
            // client=new HttpClient();
            // client.getHostConfiguration().setHost("www.hisuda.com", 80);
            // getFirst = new GetMethod("http://www.hisuda.com/sso/login");
            // // System.out.println(getFirst.getPath());
            // returnCode = client.executeMethod(getFirst);
            //
            // pageContent = getFirst.getResponseBodyAsString();
            // System.out.println(pageContent);
            // // Step 1 *************获取***************************
            // Cookie[] cookies = client.getState().getCookies();
            // for (int i = 0; i < cookies.length; i++) {
            // System.out.println("cookiename==" + cookies[i].getName());
            // System.out.println("cookieValue==" + cookies[i].getValue());
            // System.out.println("Domain==" + cookies[i].getDomain());
            // System.out.println("Path==" + cookies[i].getPath());
            // System.out.println("Version==" + cookies[i].getVersion());
            // }

            NameValuePair name = new NameValuePair("uname", "yangjie0805@sohu.com");
            NameValuePair pass1 = new NameValuePair("pwd", "111111");
            NameValuePair pass = new NameValuePair("_spring_security_remember_me", "on");
            post = new PostMethod("http://www.hisuda.com/sso/login_check");
            // 开始进行登录验证信息提交， 关键是注意参数的顺序
            post.setRequestBody(new NameValuePair[] { name, pass1, pass });
            returnCode = client.executeMethod(post);
            // post.setDoAuthentication( true );
            cookies = client.getState().getCookies();

            for (int i = 0; i < cookies.length; i++) {
                System.out.println("2cookiename==" + cookies[i].getName());
                System.out.println("2cookieValue==" + cookies[i].getValue());
                System.out.println("2Domain==" + cookies[i].getDomain());
                System.out.println("2Path==" + cookies[i].getPath());
                System.out.println("2Version==" + cookies[i].getVersion());
            }

            post = new PostMethod("http://www.hisuda.com/member/home?owner_id=52");

            returnCode = client.executeMethod(post);
            cookies = client.getState().getCookies();

            for (int i = 0; i < cookies.length; i++) {
                System.out.println("3cookiename==" + cookies[i].getName());
                System.out.println("3cookieValue==" + cookies[i].getValue());
                System.out.println("3Domain==" + cookies[i].getDomain());
                System.out.println("3Path==" + cookies[i].getPath());
                System.out.println("3Version==" + cookies[i].getVersion());
            }
            System.out.println(post.getResponseBodyAsString());

        } catch (Exception e) {

        }

    }

    public static void requestInterceptor(HttpMethod method) {
        method.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;");
        method.setRequestHeader("Accept-Language", "zh-cn");
        // αװ�����������
        method.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
        method.setRequestHeader("Accept-Charset", "UTF-8");
        method.setRequestHeader("Keep-Alive", "300");
        method.setRequestHeader("Connection", "Keep-Alive");// close
        method.setRequestHeader("Cache-Control", "no-cache");
        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }
}
